package com.yupi.springbootinit.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.exception.ThrowUtils;
import com.yupi.springbootinit.model.dto.book.BookQueryRequest;
import com.yupi.springbootinit.model.dto.order.OrderAddRequest;
import com.yupi.springbootinit.model.entity.Book;
import com.yupi.springbootinit.model.entity.Order;
import com.yupi.springbootinit.model.vo.BookVO;
import com.yupi.springbootinit.model.vo.OrderVo;
import com.yupi.springbootinit.model.vo.OrderVos;
import com.yupi.springbootinit.service.BookService;
import com.yupi.springbootinit.service.OrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wzh
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;


    @Resource
    private BookService  bookService;

    @PostMapping("/addOrder")
    public BaseResponse<OrderVo> add(@RequestBody OrderAddRequest orderAddRequest){
        OrderVo orderVo = orderService.addOrder(orderAddRequest);
        return ResultUtils.success(orderVo);
    }

    /**
     * 根据 id 获取
     *
     * @param orderNo
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<Order> getBookVOById(String orderNo, HttpServletRequest request) {
        Order orderNos = orderService.query().eq("order_no", orderNo).one();
        if (orderNos==null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"订单不存在，回显订单数据失败");
        }
        return ResultUtils.success(orderNos);
    }


    /**
     * 获取全部订单信息
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<List<OrderVos>> listBookVOByPage(HttpServletRequest request) {
        List<Order> list = orderService.query().list();
        List<OrderVos> collect = list.stream().map(order -> {
            OrderVos orderVos = OrderVos.objToVo(order);
            orderVos.setBookVO(BookVO.objToVo(bookService.getById(order.getBookId())));
            return orderVos;
        }).collect(Collectors.toList());
        return ResultUtils.success(collect);
    }


    /**
     * 获取已支付/未支付/已发货/未发货/订单信息
     * @param request
     * @return
     */
    @GetMapping("/list/page/pay")
    public BaseResponse<List<OrderVos>> listOrderVOByPage(HttpServletRequest request, Integer status) {
        List<Order> list = orderService.query().eq("order_status",status).list();
//
        List<OrderVos> collect = list.stream().map(order -> {
            OrderVos orderVos = OrderVos.objToVo(order);
            orderVos.setBookVO(BookVO.objToVo(bookService.getById(order.getBookId())));
            return orderVos;
        }).collect(Collectors.toList());
        return ResultUtils.success(collect);
    }




}
